package com.gin.no_gay_alliance.fragment.history;

import android.os.Bundle;

import com.gin.no_gay_alliance.adpter.history.ReplyHistoryAdapter;
import com.gin.no_gay_alliance.app.MyApplication;
import com.gin.no_gay_alliance.database.dao.ReplyHistoryDao;
import com.gin.no_gay_alliance.database.entity.ReplyHistory;
import com.gin.no_gay_alliance.databinding.FragmentReplyHistoryPageBinding;
import com.gin.no_gay_alliance.exception.RequireParamException;
import com.gin.no_gay_alliance.fragment.base.BaseBindingFragment;

import java.util.ArrayList;
import java.util.Comparator;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * 回复历史的一页
 *
 * @author : ginstone
 * @version : v1.0.0
 * @since : 2023/9/18 10:05
 **/
public class ReplyHistoryPageFragment extends BaseBindingFragment<FragmentReplyHistoryPageBinding> {
    private static final String ARG_KEY_TOPIC_ID = "topicId";
    private static final String ARG_KEY_PAGE = "page";
    private long topicId;
    private int page;
    private ReplyHistoryAdapter adapter;

    /**
     * onCreateView 的后续操作
     */
    @Override
    protected void onCreateView() {
        adapter = new ReplyHistoryAdapter(requireContext(), new ArrayList<>());
        b.recyclerView.setAdapter(adapter);
        adapter.updateData(query());
    }

    /**
     * 获取和处理参数
     *
     * @param arguments 参数
     */
    @Override
    protected void handleArguments(Bundle arguments) {
        if (arguments != null && arguments.containsKey(ARG_KEY_TOPIC_ID)) {
            this.topicId = arguments.getLong(ARG_KEY_TOPIC_ID);
            this.page = arguments.getInt(ARG_KEY_PAGE, 1);
        } else {
            throw new RequireParamException();
        }
    }

    /**
     * 查询本页的历史回复
     *
     * @return
     */
    private List<ReplyHistory> query() {
        ReplyHistoryDao dao = MyApplication.app.ngaDatabase.replyHistoryDao();

        List<ReplyHistory> results = dao.page(topicId, page);

        // 按照回复id分组
        Map<Long, List<ReplyHistory>> group = results.stream().collect(Collectors.groupingBy(i -> i.replyId));
        // 最终展示的数据
        List<ReplyHistory> data = new ArrayList<>();
        // 将一组回复历史合并为一个
        group.forEach((pid, list) -> {
            if (list.size() == 1) {
                // 只有一个版本，直接放入
                data.add(list.get(0));
            } else {
                // 有多个版本，取最新版本，其他版本放入"历史版本"字段
                ReplyHistory latest = list.get(list.size() - 1);
                latest.versions = list.subList(0, list.size() - 1);
                data.add(latest);
            }
        });
        data.sort(Comparator.comparingInt(o -> o.floorNumber));
        return data;
    }

    public static ReplyHistoryPageFragment newInstance(long topicId, int page) {
        ReplyHistoryPageFragment fragment = new ReplyHistoryPageFragment();
        Bundle args = new Bundle();
        args.putLong(ARG_KEY_TOPIC_ID, topicId);
        args.putInt(ARG_KEY_PAGE, page);
        fragment.setArguments(args);
        return fragment;
    }
}
